package org.jctc.els.modules.async.service.impl;
import java.util.Date;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jctc.els.common.constant.ConfigConstant;
import org.jctc.els.modules.async.service.IAsyncService;
import org.jctc.els.modules.biz.entity.Config;
import org.jctc.els.modules.biz.entity.districts.Districts;
import org.jctc.els.modules.biz.entity.districts.DistrictsUtil;
import org.jctc.els.modules.biz.service.config.IConfigService;
import org.jctc.els.modules.biz.service.districts.IDistrictsService;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

/**
 * 异步方法合集
 *
 * @author Rxy
 * @date 2022/3/2 15:55
 */
@Service
@EnableAsync
public class AsyncServiceImpl implements IAsyncService {

	@Autowired
	private IDistrictsService districtsService;
	@Autowired
	private IConfigService configService;
	public static final String LEVEL_NAME_COUNTRY = "country";
	public static final String LEVEL_NAME_PROVINCE = "province";
	public static final String LEVEL_NAME_CITY = "city";
	public static final String LEVEL_NAME_STREET = "street";

	@Override
	@Async
	public String synDistrictToDataBaseGaoDe(Long userId) {
		String geoInfo = DistrictsUtil.getDistrictData();
		String md5Value = Func.md5Hex(geoInfo);
		Config config = configService.selectByName(ConfigConstant.KEY_ALI_DISTRICTS_VERSION);
		String version = config.getValuej();
		if(Func.equals(md5Value,version)){
			return "未找到需要同步的数据！";
		}
		JSONObject jsonObject = null;
		try {
			jsonObject = JSONObject.parseObject(geoInfo);
		} catch (Exception e) {

		}
		if (Func.isEmpty(jsonObject)) {
			return "获取数据异常！";
		}
		JSONArray jsonArray = jsonObject.getJSONArray("districts");
		//第一级是中国
		JSONObject countryObject =  (JSONObject) jsonArray.get(0);
		Districts country = createDistrict(LEVEL_NAME_COUNTRY,userId,countryObject,null,null);
		districtsService.importData(country);

		//第二级是省份
		JSONArray provinceArray = countryObject.getJSONArray("districts");
		if(Func.isNotEmpty(provinceArray)){
			for (int i = 0; i < provinceArray.size(); i++) {
				JSONObject provinceObject = provinceArray.getJSONObject(i);
				Districts province = createDistrict(LEVEL_NAME_PROVINCE,userId,provinceObject,countryObject,countryObject);
				districtsService.importData(province);

				//第三级是城市
				JSONArray cityArray = provinceObject.getJSONArray("districts");
				if(Func.isNotEmpty(cityArray)){
					for (int j = 0; j < cityArray.size(); j++) {
						JSONObject cityObject = cityArray.getJSONObject(j);
						Districts city = createDistrict(LEVEL_NAME_CITY,userId,cityObject,provinceObject,countryObject);
						districtsService.importData(city);
						//第四级是区县
						JSONArray streetArray = cityObject.getJSONArray("districts");
						if(Func.isNotEmpty(streetArray)){
							for (int z = 0; z < streetArray.size(); z++) {
								JSONObject streetObject = streetArray.getJSONObject(z);
								Districts street = createDistrict(LEVEL_NAME_STREET,userId,streetObject,cityObject,countryObject);
								districtsService.importData(street);
							}
						}
					}
				}
			}
		}
		return "OK";
	}

	/**
	 *
	 * @author Rxy
	 * @date 2022/3/2 16:53
	 * @param
	 * @param level country:国家  province：省份  city：城市
	 * @param userId
	 * @param jsonObject
	 * @return Districts
	 */
	private Districts createDistrict(String level,Long userId,JSONObject jsonObject,JSONObject parentObject,JSONObject provinceObject){
		if(Func.isEmpty(jsonObject)){
			return null;
		}
		Districts districts = new Districts();
		if(LEVEL_NAME_COUNTRY.equals(level)) {
			districts.setProvinceCode(jsonObject.getString("adcode"));
			districts.setCityCode("");
		}
		if(LEVEL_NAME_CITY.equals(level)) {
			districts.setProvinceCode(parentObject.getString("adcode"));
			districts.setCityCode(jsonObject.getString("adcode"));
		}
		if(LEVEL_NAME_STREET.equals(level)) {
			districts.setProvinceCode(provinceObject.getString("adcode"));
			districts.setCityCode(parentObject.getString("adcode"));
		}
		districts.setAdCode(jsonObject.getString("adcode"));
		districts.setName(jsonObject.getString("name"));
		districts.setLevel(jsonObject.getString("level"));
		districts.setCenter(jsonObject.getString("center"));
		districts.setLat(districts.getCenter().split(",")[0]);
		districts.setLng(districts.getCenter().split(",")[1]);
		districts.setCreatedBy(userId);
		districts.setCreatedTime(new Date());
		districts.setUpdatedBy(userId);
		districts.setUpdatedTime(new Date());

		return districts;
	}
}
